package com.zzq.service.impl;

import com.zzq.mapper.BidderInfoMapper;
import com.zzq.mapper.MessageMapper;
import com.zzq.mapper.TendererInfoMapper;
import com.zzq.mapper.TendererMapper;
import com.zzq.pojo.BidInfo;
import com.zzq.pojo.vo.BidStatusUpdate;
import com.zzq.pojo.vo.InformName;
import com.zzq.service.BidderInfoService;
import com.zzq.tools.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

@Service
@Slf4j
public class BidderInfoServiceImpl implements BidderInfoService {

    @Autowired
    private BidderInfoMapper bidderInfoMapper;

    @Autowired
    private TendererInfoMapper tendererInfoMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${rocketmq-topic.bidding}")
    private String biddingTopic;

    @Value("${rocketmq-topic.inform}")
    private String winningTopic;

//    // 获取自定义线程池
//    ExecutorService executor = ThreadPoolUtil.getExecutor();

    @Override
    @Transactional
    public void add(BidInfo bidInfo) {
        Double bidAmount=bidInfo.getBidAmount();
        Double BidDeposit=tendererInfoMapper.getBidDeposit(bidInfo.getTenderProjectNo());
        if(bidAmount<BidDeposit){
            throw new RuntimeException("投标金额不能低于投标保证金");
        }
        bidInfo.setStatus("评选中");
        bidderInfoMapper.add(bidInfo);
        tendererInfoMapper.addBidderCount(bidInfo.getTenderProjectNo());
    }

    @Override
    public ArrayList<BidInfo> recommend(String tenderer) {

        return bidderInfoMapper.recommend(tenderer);
    }

    @Override
    @Transactional
    public void updateBidStatus(List<BidStatusUpdate> bidStatusUpdates) {

        String winnerId="";

        for (BidStatusUpdate update : bidStatusUpdates) {
            bidderInfoMapper.updateBidStatus(update.getId(), update.getStatus());
            if("中标".equals(update.getStatus())){
                winnerId=update.getId();
            }
        }

        BidStatusUpdate ex= bidStatusUpdates.get(0);
        String tenderProjectNo=bidderInfoMapper.getTenderProjectNoById(ex.getId());
        tendererInfoMapper.updateStatus(tenderProjectNo);
        String finalWinnerId = winnerId;

        InformName informName=bidderInfoMapper.getNameById(finalWinnerId);
        String message="【"+informName.getTenderer()+"】的项目【"+informName.getTenderProjectNo()+"】的中标方为【"+informName.getBidder()+"】，请及时联系对方。";

        rocketMQTemplate.convertAndSend(
                winningTopic + ":INFORM_TOPIC",
                message);

        log.info("通知发送成功: {}" ,message);

//        executor.submit(() -> {
//            InformName informName=bidderInfoMapper.getNameById(finalWinnerId);
//            String message="【"+informName.getTenderer()+"】的项目【"+informName.getTenderProjectNo()+"】的中标方为【"+informName.getBidder()+"】，请及时联系对方。";
//            messageMapper.pushMessage(message);
//        });


    }

    @Override
    public List<BidInfo> myBidderInfo(String name) {
        return bidderInfoMapper.myBidderInfo(name);
    }
}
